package io;

import java.io.*;

/**
 * @description: 图片复制
 * @author apple
 * @date 2021/9/12 12:58 上午
 * @version 1.0
 */
public class ImageDemo {

    public static void main(String[] args) {
        //  字符复制图片，失败
        characterIn();
        //  字节复制图片，成功
        byteIn();
    }

    public static void byteIn() {
        File src = new File("1.jpeg");
        File desc = new File("3.jpeg");

        InputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(src);
            out = new FileOutputStream(desc);
            byte[] buffs = new byte[1024];
            int len = 0;
            while ((len = in.read(buffs)) != -1) {
                out.write(buffs);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static void characterIn() {
        Reader in = null;
        Writer out = null;
        try {
            in = new FileReader("1.jpeg");
            out = new FileWriter("2.jpeg");
            char[] buffs = new char[1024];
            int length = 0;
            while ((length = in.read(buffs)) != -1) {
                out.write(buffs);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
